//
//  RBCompeCalendarView.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/12.
//

#import "RBCompeCalendarView.h"
#import "RBCompeCalendarCollecCell.h"
#import "RBCompeCalendarModel.h"

@interface RBCompeCalendarView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic)UIView *bgView;
@property(strong,nonatomic)UICollectionView *collectionView;
@end

@implementation RBCompeCalendarView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self rb_initViewUI];
        [self layoutIfNeeded];
    }
    return self;
}
-(void)rb_initViewUI{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.collectionView];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
}
-(void)setWeekArray:(NSMutableArray *)weekArray{
    _weekArray=weekArray;
    [self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.weekArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RBCompeCalendarCollecCell *compeCalendarCollecCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RBCompeCalendarCollecCell" forIndexPath:indexPath];
    compeCalendarCollecCell.model = self.weekArray[indexPath.item];
    return compeCalendarCollecCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (RBCompeCalendarModel *model in self.weekArray) {
        model.isSelectDate = NO;
    }
    RBCompeCalendarModel *selectComCalendarModel = self.weekArray[indexPath.row];
    selectComCalendarModel.isSelectDate = YES;
    if(self.didSelectItemAtIndexPathBlock){
        self.didSelectItemAtIndexPathBlock(selectComCalendarModel);
    }
    [self.collectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(RBScaleWidth(75), RBScaleWidth(44));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[RBCompeCalendarCollecCell class] forCellWithReuseIdentifier:NSStringFromClass([RBCompeCalendarCollecCell class])];
    }
    return _collectionView;
}
@end
